class GameAside {
    id = 0;
    contentText: renderer.TextField;
    contentTexts: renderer.TextField[] = [];
    contents: string[] = []
    imageUrls: string[] = [];
    npcDialogTexture = new Image();

    constructor(config: any) {
        this.id = config.id;
        this.contents = config.texts;
        this.imageUrls = config.images;
    }
}


class GameAsideManager {
    gameasides: GameAside[] = [];

    parseFromConfig(obj: any) {
        for (let item of obj.gameaside) {
            const gameaside = new GameAside(item)
            this.gameasides.push(gameaside)
        }
    }
    getAsideById(id: number) {
        for (let gameaside of this.gameasides) {
            if (gameaside.id == id) {
                return gameaside;
            }
        }
        throw `missing gameasideid : ${id}`;
    }

}

class AsideDialog extends renderer.DisplayObjectContainer {

    private i = 0;

    contentText: renderer.TextField;
    contentTexts: renderer.TextField[] = [];

    contents: string[] = [];


    imageUrls: string[] = [];

    asideDialogTexture = new Image();


    constructor(gameaside: GameAside) {
        super();

        this.removeAllChildren();
        this.contents = gameaside.contents;
        console.log(this.contents)

        // console.log(gameaside.contentTexts + "123455")
        // console.log(this.contentTexts, "aaaaaaaaaaabbbb")
        this.imageUrls = gameaside.imageUrls;
        console.log(this.contents);


        const dialogBackgroundTexture = new Image();
        dialogBackgroundTexture.src = 'dialog.png';
        const background = new renderer.Bitmap(0, 416, dialogBackgroundTexture);

        const asideNpc = new renderer.Bitmap(0, 128, this.asideDialogTexture);

        this.addChild(asideNpc);
        this.addChild(background);


        this.contentText = new renderer.TextField();
        this.addChild(this.contentText);
        this.contentText.x = 80;
        this.contentText.y = 464;

        this.addEventListener("click", (eventData: any) => {
            this.i++;
            this.update();
        });
        this.update();
    }
    update() {
        let contents: string[] = [];
        let imageUrls: string[] = [];
        contents = this.contents;
        imageUrls = this.imageUrls;
        if (this.i >= contents.length) {
            this.dispatchEvent("close", null);
        }
        else {
            this.contentText.text = contents[this.i]
            // this.imageId = mission.images[this.i];
            this.asideDialogTexture.src = imageUrls[this.i];
        }
    }
}

const gameAsideManager = new GameAsideManager();